//
//  DayHeaderView.swift
//  ImGuider X
//
//  Created by 王鹏宇 on 10/22/18.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

protocol DayHeaderViewDelegate : class {
    
    func goMapVC(day:Day?)
}

class DayHeaderView : UITableViewHeaderFooterView {

    var cityNameLabel : UILabel!
    var dateLabel : UILabel!
    var mapBtn : UIButton!
    var dayLabel : UILabel!
    
    weak var delegate : DayHeaderViewDelegate?
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    
    func setup(){
        
        dayLabel = UILabel()
        self.contentView.addSubview(dayLabel)
        dayLabel.snp.makeConstraints { (make) in
            make.top.bottom.left.equalToSuperview()
            make.width.equalTo(79)
        }
        dayLabel.textAlignment = .center
        dayLabel.textColor = kSubitleColor
        dayLabel.font = UIFont.systemFont(ofSize: 24)
        
        
//        let lineView = UIView()
//        lineView.backgroundColor = UIColor.hexStringToColor(hexString:"#E2E2E2")
//        self.contentView.addSubview(lineView)
//        lineView.snp.makeConstraints { (make) in
//            make.top.bottom.equalToSuperview()
//            make.left.equalTo(self.contentView.snp.left).offset(76)
//            make.width.equalTo(1)
//        }
//
//        let circleView = UIView()
//        circleView.layer.cornerRadius = 4
//        circleView.backgroundColor = UIColor.hexStringToColor(hexString: "#4A4A4A")
//        self.contentView.addSubview(circleView)
//        circleView.snp.makeConstraints { (make) in
//            make.centerX.equalTo(lineView.snp.centerX)
//            make.centerY.equalTo(self.contentView.snp.centerY)
//            make.width.height.equalTo(8)
//        }
        
        
        self.contentView.backgroundColor = UIColor.white

    
        
        mapBtn = UIButton()
//        mapBtn.setImage(UIImage(named: "导航1"), for: UIControl.State.normal)
        mapBtn.setTitle(LocalizedString("speech_map"), for: UIControl.State.normal)
        mapBtn.setTitleColor(UIColor.hexStringToColor(hexString: "#0ECEA6"), for: UIControl.State.normal)
        mapBtn.titleLabel?.font = kFont14
        mapBtn.addTarget(self, action: #selector(goMap), for: UIControl.Event.touchUpInside)
        mapBtn.addShadow(type: ShadowType.btn)
        self.contentView.addSubview(mapBtn)
        mapBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.contentView.snp.centerY)
   make.trailing.equalTo(self.contentView.snp.trailing).offset(-16)
            make.width.equalTo(60)
            make.height.equalTo(40)
        }
        
        dateLabel = UILabel()
        dateLabel.font = UIFont.systemFont(ofSize: 12)
        dateLabel.textColor = kContentColor
        dateLabel.text = ""
        self.contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(self.contentView.snp.leading).offset(108)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-5)
            make.height.equalTo(21)
        }
        
        cityNameLabel = UILabel()
        cityNameLabel.font = UIFont.systemFont(ofSize: 17)
        cityNameLabel.textColor = kTitleColor
        cityNameLabel.text = ""
        self.contentView.addSubview(cityNameLabel)
        cityNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.contentView.snp.top).offset(10)
            make.leading.equalTo(self.contentView.snp.leading).offset(108)
            make.bottom.equalTo(self.dateLabel.snp.top).offset(0)
        }
    }
    
    var day:Day? {
        
        didSet{
            
            self.cityNameLabel.text = day?.inCity
            
            guard let dateStr = day?.date else {return}
            
            let date = Date.date(dateStr, "yyyy-MM-dd")
            
            
            var formatDate = date?.string("yyyy年MM月dd日 EEE",language: .chinese)
            
            if AppManager.share.language == .english {
                
                formatDate = date?.string("dd MMM yyyy EEE",language: .english)
            }
            
            self.dateLabel.text = formatDate
        }
    }
    
   @objc func goMap() {
    
       delegate?.goMapVC(day: self.day)
    }
}
